<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir . "/inc/vertraege.php";
require_once $basedir . "/inc/mitglieder.php";
$temp_adressen = array();
$contracts = Vertraege::getAll(true, '2013-08-01');
foreach($contracts as $value) {
    $mutter = $value->getKind()->getMutter();
    $vater = $value->getKind()->getVater();
    if($mutter->isEMailErlaubt()) {
        array_push($temp_adressen, $value->getKind()->getMutter());
    } else if($vater->isEMailErlaubt()) {
        array_push($temp_adressen, $value->getKind()->getVater());
    } else {
        array_push($temp_adressen, $value->getKind()->getMutter());
    }
}
$members = Mitglieder::getAll(true, '2013-08-01');
foreach($members as $value) {
    array_push($temp_adressen, $value->getAdress());
}
$adressen = array();
for($i = 0; $i < count($temp_adressen); $i++) {
    if($temp_adressen[$i]->isEMailErlaubt()) {
        $exist = false;
        for($x = 0; $x < count($adressen); $x++) {
            if($adressen[$x]->getId() == $temp_adressen[$i]->getId()) {
                $exist = true;
                break;
            }
        }
        if(!$exist) {
            array_push($adressen, $temp_adressen[$i]);
        }
    }
}

if ($format == "excel") {
    require_once $basedir . "/external/PHPExcel.php";

    $objPHPExcel = new PHPExcel();
    PHPExcel_Settings::setLocale("de");
    $worksheet = $objPHPExcel->getActiveSheet();
    /*
     * Create Table Header
     */
    $worksheet->setCellValueByColumnAndRow(0, 1, "Anrede");
    $worksheet->setCellValueByColumnAndRow(1, 1, "Name");
    $worksheet->setCellValueByColumnAndRow(2, 1, "Vorname");
    $worksheet->setCellValueByColumnAndRow(3, 1, "Straße");
    $worksheet->setCellValueByColumnAndRow(4, 1, "Nr.");
    $worksheet->setCellValueByColumnAndRow(5, 1, "Zus.");
    $worksheet->setCellValueByColumnAndRow(6, 1, "PLZ");
    $worksheet->setCellValueByColumnAndRow(7, 1, "Ort");
    $worksheet->setCellValueByColumnAndRow(8, 1, "EMail");
    /*
     * Create Content to Table
     */
    $row = 0;
    for ($i = 0; $i < count($adressen); $i++) {
        $value = $adressen[$i];
        $row = $i + 2;
        $worksheet->setCellValueByColumnAndRow(0, $row, utf8_encode($value->getAnrede()));
        $worksheet->setCellValueByColumnAndRow(1, $row, utf8_encode($value->getNachname()));
        $worksheet->setCellValueByColumnAndRow(2, $row, utf8_encode($value->getVorname()));
        $worksheet->setCellValueByColumnAndRow(3, $row, utf8_encode($value->getStrasse()));
        $worksheet->setCellValueByColumnAndRow(4, $row, utf8_encode($value->getHausnummer()));
        $worksheet->setCellValueByColumnAndRow(5, $row, utf8_encode($value->getZusatzHausnummer()));
        $worksheet->setCellValueByColumnAndRow(6, $row, utf8_encode($value->getPlz()));
        $worksheet->setCellValueByColumnAndRow(7, $row, utf8_encode($value->getOrt()));
        $worksheet->setCellValueByColumnAndRow(8, $row, utf8_encode($value->getEMail()));
    }
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    exit ;
}
?>
